from rest_framework import serializers
from apk.models import *

# 序列化器的文件，对存放在数据库中的数据在传送到前端去时做校验：序列化操作

class SignatureSerializer(serializers.ModelSerializer):
    class Meta:
        model = apk_signature
        fields = '__all__'


class AndroidmanifestAPKSerializer(serializers.ModelSerializer):
    class Meta:
        model = apk_AndroidmanifestAPK
        fields = '__all__'


class DexInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = apk_dexInfo
        fields = '__all__'


class APK_filesSerializer(serializers.ModelSerializer):

    class Meta:
        model = apk_files
        fields = '__all__'

class APK_related_sitesSerializer(serializers.ModelSerializer):

    class Meta:
        model = apk_related_site
        fields = '__all__'

class black_apkSerializer(serializers.ModelSerializer):

    class Meta:
        model = black_apk
        fields = '__all__'

class white_apkSerializer(serializers.ModelSerializer):

    class Meta:
        model = white_apk
        fields = '__all__'

class APK_infoSerializer(serializers.ModelSerializer):
    signature = SignatureSerializer()
    AndroidmanifestAPK = AndroidmanifestAPKSerializer()
    dexInfo = DexInfoSerializer()
    files = APK_filesSerializer()
    related_site = APK_related_sitesSerializer()

    class Meta:
        model = APK_info
        fields = '__all__'

    def create(self, validated_data):
        signature_data = validated_data.pop('signature')
        AndroidmanifestAPK_data = validated_data.pop('AndroidmanifestAPK')
        dexInfo_data = validated_data.pop('dexInfo')
        APK_files_data = validated_data.pop('files')
        related_sites_data = validated_data.pop('related_site')

        signature = apk_signature.objects.create(**signature_data)
        androidmanifestAPK = apk_AndroidmanifestAPK.objects.create(**AndroidmanifestAPK_data)
        dexInfos = apk_dexInfo.objects.create(**dexInfo_data)
        files = apk_files.objects.create(**APK_files_data)
        related_site = apk_related_site.objects.create(**related_sites_data)

        apk_info = APK_info.objects.create(
            signature=signature,
            androidmanifestAPK=androidmanifestAPK,
            dexInfos=dexInfos,
            files=files,
            related_site=related_site,
            **validated_data
        )
        return apk_info

    def update(self, instance, validated_data):
        signature_data = validated_data.pop('signatures')
        AndroidmanifestAPK_data = validated_data.pop('androidmanifestAPK')
        dexInfo_data = validated_data.pop('dexInfos')
        APK_files_data = validated_data.pop('apk_files')
        related_sites_data = validated_data.pop('related_sites')


        apk_signature.objects.filter(pk=instance.signature.pk).update(**signature_data)
        apk_AndroidmanifestAPK.objects.filter(pk=instance.AndroidmanifestAPK.pk).update(**AndroidmanifestAPK_data)
        apk_dexInfo.objects.filter(pk=instance.dexInfo.pk).update(**dexInfo_data)
        apk_files.objects.filter(pk=instance.files.pk).update(**APK_files_data)
        apk_related_site.objects.filter(pk=instance.related_site.pk).update(**related_sites_data)


        instance.additional_field = validated_data.get('additional_field', instance.additional_field)
        instance.save()

        return instance
